package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.ClueStatusConstants;
import com.qk.domain.clue.ClueDO;
import com.qk.domain.clue.CluePoolDTO;
import com.qk.dto.clue.ClueDTO;
import com.qk.dto.clue.ClueListDTO;
import com.qk.dto.clue.MarkFalseClueDTO;
import com.qk.dto.clue.UpdateClueInfoDTO;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.ClueService;
import com.qk.management.service.RoleService;
import com.qk.vo.clue.CluePoolVO;
import com.qk.vo.clue.ClueVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author: Enid
 * @date: 2025/10/13 17:32
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/clues")
@SuppressWarnings("all")

public class ClueController {
    @Autowired
    private ClueService clueService;
    @Autowired
    private RoleService roleService;

    @GetMapping("/pool")
    public Result pool(CluePoolDTO dto) {
        PageResult<CluePoolVO> pool = clueService.poolList(dto);
        return Result.success(pool);
    }
    //伪线索
    @PutMapping("/false/{id}")
    public Result falseClue(@PathVariable Integer id, @RequestBody MarkFalseClueDTO dto) {
        clueService.toFake(id,dto);
        return Result.success();
    }




    /**
     * 转线索--改变线索状态，并将信息添加到商机表中
     */
    @LogAnno
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id) {
        clueService.toBusiness(id);
        return Result.success();
    }

    /**
     * 线索跟进
     */
    @LogAnno
    @PutMapping
    public Result updateClueInfoById(@RequestBody UpdateClueInfoDTO dto) {
        clueService.updateClueInfoById(dto);
        return Result.success();
    }

    /**
     * 查询回显
     */
    @GetMapping("/{id}")
    public Result getclueInfoById(@PathVariable Integer id) {
        ClueVO vo=clueService.getClueInfoById(id);
        return Result.success(vo);
    }

    /**
     * 线索分配
     * @param clueId
     * @param userId
     * @return
     */
    @LogAnno
    @PutMapping("/assign/{clueId}/{userId}")
    public Result updateClueById(@PathVariable Integer clueId, @PathVariable Integer userId) {
        clueService.updateById(Clue.builder()
                .id(clueId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(ClueStatusConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();
    }


    @GetMapping
    public Result list(ClueListDTO clueListDTO) {
        PageResult<ClueDO> pageResult = clueService.cluesList(clueListDTO);
        return Result.success(pageResult);
    }

    @LogAnno
    @PostMapping
    public Result save(@RequestBody ClueDTO clueDTO) {
        clueService.addClue(clueDTO);
        return Result.success();
    }
}
